package com.weakteam.mobile.minimum.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.weakteam.mobile.minimum.R;
import com.weakteam.mobile.minimum.fragment.MenuNavigationScreen;
import com.weakteam.mobile.minimum.fragment.ProfileScreen;
import com.weakteam.mobile.minimum.ui.widget.MenuLayout;

/**
 * Created by Nail Sharipov (nailxsharipov@gmail.com) on 10.11.2014.
 */
public class Main extends Activity {

    private FrameLayout content;
    private MenuLayout menuLayout;

    private Fragment currentFragment;
    private MenuNavigationScreen menuNavigation;
    private ProfileScreen profile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.menuLayout = (MenuLayout)this.getLayoutInflater().inflate(R.layout.activity_main, null);
        setContentView(this.menuLayout);

        this.content = (FrameLayout)findViewById(R.id.menu_content);
        this.setMenuNavigation();
    }

    private void setMenuNavigation() {
        if (this.menuNavigation == null) {
            this.menuNavigation = new MenuNavigationScreen();
        }
        this.setFragment(this.menuNavigation);
    }

    public void setProfile() {
        if (this.profile == null) {
            this.profile = new ProfileScreen();
        }
        this.setFragment(this.profile);
    }


    private void setFragment(final Fragment fragment) {
        if (fragment != null && fragment != this.currentFragment) {
            final FragmentManager manager = this.getFragmentManager();
            if (this.currentFragment != null) {
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_in_right);

                transaction.replace(this.content.getId(), fragment);
                transaction.addToBackStack(null);
                transaction.commit();
            } else {
                final String name = fragment.getClass().getName();
                manager.beginTransaction().add(this.content.getId(), fragment, name).commit();
            }
            /*
            final String name = fragment.getClass().getName();
            manager.beginTransaction().add(this.content.getId(), fragment, name).commit();
            */
        }
        this.currentFragment = fragment;
    }
}
